package m.fun.action;

import m.base.action.BaseAction;
import m.base.action.StoreInfoAction;
import m.base.util.LevelUtil;
import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.fun.model.GoodsOffer;
import m.fun.model.OfferDetail;
import m.fun.service.OfferDetailService;
import m.fun.service.OfferInfoService;
import m.html.VueFormResult;
import m.html.VueListResult;
import m.html.element.Element;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.element.other.Param;
import m.html.form.*;
import m.html.list.*;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import manage.model.StatusModel;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@ActionMeta(name = "fun/offerDetail")
public class OfferDetailAction extends BaseAction {
    private OfferDetail model;
    private List<GoodsOffer> goodsList;
    @RequestMeta(params = {"offerOid"})
    public JSONMessage getList(String offerOid) throws SQLException, MException {
        return new JSONMessage(0)
            .push("list",
                ModelQueryList.getModelList(OfferDetail.class,
                    new String[]{"goodsOffer.*","goodsOffer.goods.storeType.name","goodsOffer.goods.name","goodsOffer.goods.offerPrice"},
                    null,
                    QueryCondition.eq("offer.oid",offerOid),
                    QueryOrder.asc("goodsOffer.goods.storeType.sort"),
                    QueryOrder.asc("goodsOffer.title")
                )
            );
    }
    @RequestMeta(logTitle = "报价单明细",logDescription = "添加报价单明细",params = {"offerOid"},transaction = true)
    public JSONMessage doAddGoods(String offerOid) throws MException, SQLException {
        getService(OfferDetailService.class).addGoodsOffer(offerOid,goodsList);
        return new JSONMessage(0,"添加成功");
    }
    @RequestMeta(logTitle = "报价单明细",logDescription = "删除报价单明细",transaction = true)
    public JSONMessage doDelete() throws Exception {
        getService(OfferDetailService.class).delete(model.getOid());
        return new JSONMessage(0,"删除成功");
    }
    /*
     * 编辑商品报价
     */
    @RequestMeta(params = {"offerOid"})
    public VueResult toAdd(String offerOid) throws Exception {
        ModelForm form = new ModelForm("批量添加商品报价");
        form.addItem(
            FormInput.selectManyModel("添加商品报价","list","goodsList","'"+actionUrl("selectGoodsOffer")+"?params[offerOid]="+offerOid+"'", Element.tag("div").A("v-for","item in list.goodsList").E("{{item.goods.name}}").toString()).setSpan(3)
        )
        .addData("list",new JSONMessage().push("goodsList",new ArrayList<>()))
        .addButton(
            FormButton.pri("确定添加","doAddGoods")
        )
        .addMethod(
            FormEvent.backFlag,
            FormEvent.doJson("doAddGoods",actionUrl("doAddGoods"),"{offerOid:'"+offerOid+"',goodsList:this.list.goodsList}")
                .success("backFlag")
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form)
                .addForm(new CustomForm(Element.tag("a-alert").A("message","先维护商品报价才能在这里选择。")));
    }
    public VueResult selectGoodsOffer() throws Exception {
        String selected=getParamsValue("selected","");
        String offerOid=getParamsValue("offerOid","");
        VueListResult result=getVueListResult("选择商品报价", GoodsOffer.class,actionUrl("selectGoodsOffer"));
        result.setDescribe("<a-alert :message=\"'已选择'+sumSelected(item=>1)+'种商品报价。'\" style=\"margin-bottom:10px;\" />");
        result.setFunction(LevelUtil.exportFunc(),false);
        result.addQueryFields("oid","color");
        result.setSelection("oid", false);
        result.setHeight(-1);
        result.addCol(toIndexColumn());
        result.addCol(toTextColumn("商品分类", "goods.storeType.name", 100, true));
        result.addCol(toTextColumn("商品名称", "goods.name", 250, true));
        result.addCol(toNumberColumn("报价","goods.offerPrice",100,true));
        result.addCol(toNumberColumn("均价", "goods.surplusPrice", 100, true).setGroupTitle("剩余"));
        result.addCol(toIntegerColumn("数量", "goods.surplusNum", 100, true).setGroupTitle("剩余"));
        result.addCol(toNumberColumn("金额", "goods.surplusAmount", 100, true).setGroupTitle("剩余"));
        result.addCol(toTextColumn("展示名称", "title", 250, true).addClassName("record.color","'text-'+dict('color',record.color)"));
        result.addCol(toTextColumn("展示标识", "indexTxt", 100, true).setDictType("m.fun.model.GoodsOffer!indexTxt").setFixed(Align.RIGHT));
        result.addCol(toTextColumn("展示价格", "priceTxt", 100, true).setFixed(Align.RIGHT));
        result.addButton(ListButton.back("选择", ButtonType.PRIMARY).addParam("",""));
        //排序 result.addOrder
        //查询条件 result.addCondition
        result.addCondition(QueryCondition.eq("user.oid",PowerUtil.getSessionUserOid()));
        result.addCondition(QueryCondition.eq("goods.status", StatusModel.Status_Enable));
        result.addCondition(QueryCondition.not(
            QueryCondition.in("oid",
                ModelQueryList.instance(OfferDetail.class,new String[]{"goodsOffer.oid"},null,
                    QueryCondition.eq("offer.oid",offerOid)
                )
            )
        ));
        result.addOrder(
            QueryOrder.asc("goods.storeType.sort"),
            QueryOrder.asc("title")
        );
        return result;
    }
    /*
     * 商品报价列表
     */
    public VueResult detailList() throws Exception {
        VueListResult result=getVueListResult("商品报价", OfferDetail.class,actionUrl("detailList"));
        result.setFunction(true,false);
        result.setHeight(-1);
        result.addQueryFields("goodsOffer.color");
        result.setSelection("oid",false);
        result.addCol(toIndexColumn());
        result.addCol(toTextColumn("商品分类", "goodsOffer.goods.storeType.name", 100, true));
        result.addCol(toTextColumn("商品名称", "goodsOffer.goods.name", 250, true));
        result.addCol(toNumberColumn("报价", "goodsOffer.goods.offerPrice", 100, true));
        result.addCol(toIntegerColumn("数量", "goodsOffer.goods.surplusNum", 100, true).setGroupTitle("剩余"));
        result.addCol(toNumberColumn("金额", "goodsOffer.goods.surplusAmount", 100, true).setGroupTitle("剩余"));
        result.addCol(toNumberColumn("均价", "goodsOffer.goods.surplusPrice", 100, true).setGroupTitle("剩余"));
        result.addCol(toTextColumn("展示名称", "goodsOffer.title", 250, true).addClassName("record.goodsOffer.color","'text-'+dict('color',record.goodsOffer.color)"));
        result.addCol(toTextColumn("展示标识", "goodsOffer.indexTxt", 100, true).setDictType("m.fun.model.GoodsOffer!indexTxt"));
        result.addCol(toTextColumn("展示价格", "goodsOffer.priceTxt", 100, true));
        result.addButton(
            ListButton.modal("添加", ButtonType.PRIMARY,actionUrl("toAdd"),600, SuccessMethod.REFRESH)
                .addQueryParam(Param.value("offerOid",getParamsValue("offer.oid"))),
            ListButton.ajax("删除", ButtonType.WARN,actionUrl("doDelete"), SuccessMethod.REFRESH)
                .addParam("model.oid","oid").setConfirm("确定要删除吗？")
        );
        //排序 result.addOrder
        //查询条件 result.addCondition
        result.addSearch(ListSearch.field("","offer.oid", SearchType.HIDDEN_EQ));
        result.addOrder(
            QueryOrder.asc("goodsOffer.goods.storeType.sort"),
            QueryOrder.asc("goodsOffer.title")
        );
        return result;
    }

    public OfferDetail getModel() {
        return model;
    }

    public void setModel(OfferDetail model) {
        this.model = model;
    }

    public List<GoodsOffer> getGoodsList() {
        return goodsList;
    }

    public void setGoodsList(List<GoodsOffer> goodsList) {
        this.goodsList = goodsList;
    }
}
